#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);

	ps->arr = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void StackDestory(Stack* ps)
{
	assert(ps);

	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = 0;
	ps->top = 0;
	ps = NULL;
}

void StackCheckCapacity(Stack* ps)
{
	assert(ps);

	int newcapacity = ps->capacity == 0 ? 5 : 2 * ps->capacity;
	SDataType* pn = (SDataType*)realloc(ps->arr, sizeof(SDataType) * newcapacity);
	if (pn)
	{
		ps->arr = pn;
		ps->capacity = newcapacity;
	}
	else
	{
		perror("StackCheckCapacity");
		return;
	}
}

void StackPush(Stack* ps, SDataType x)
{
	assert(ps);

	if (ps->capacity == ps->top)
		StackCheckCapacity(ps);
	ps->arr[ps->top] = x;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);

	ps->top--;
}

SDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);

	return ps->arr[ps->top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);

	return ps->top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);

	return ps->top == 0;
}